package com.example.reader.ui.main.widget;

import android.app.Activity;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPropertyAnimatorListenerAdapter;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.example.reader.R;

/**
 * Created by Administrator on 2017/3/10.
 */

public class BookshelfBookInfoWindow {
    private static final String TAG = "BookshelfBookInfoWindow";


    private final PopupWindow window;
    private final View rl_bg;
    private final View ll_content;
    private View parent;
    private int height;

    BookshelfBookInfoWindow(Activity activity, View parent) {
        this.parent = parent;
        window = new PopupWindow();
        window.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        window.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        window.setFocusable(true);
        View inflate = LayoutInflater.from(activity).inflate(R.layout.pop_manage_book, null);
        ll_content = inflate.findViewById(R.id.ll_content);
        rl_bg = inflate.findViewById(R.id.rl_bg);
        rl_bg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                close();
            }
        });
        window.setContentView(inflate);
    }

    public static BookshelfBookInfoWindow getWindow(Activity activity, View parent) {
        BookshelfBookInfoWindow window = new BookshelfBookInfoWindow(activity, parent);
        return window;
    }

    public void show() {
        ll_content.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        height = ll_content.getMeasuredHeight();
        Log.i(TAG, "show: height is" + height);
        ViewCompat.setTranslationY(ll_content, height);
        ViewCompat.animate(ll_content)
                .translationY(0)
                .setDuration(250)
                .start();
        window.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
    }

    public void close() {
        ViewCompat.animate(rl_bg)
                .setDuration(250)
                .alpha(0)
                .start();

        ViewCompat.animate(ll_content)
                .translationY(height)
                .setDuration(250)
                .setListener(new ViewPropertyAnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(View view) {
                        super.onAnimationEnd(view);
                        window.dismiss();
                    }
                })
                .start();
    }

}
